﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace WindowsFormsApp1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            var name = textBox1.Text;
            var sql = string.Format("select * from Articles where Title like'%{0}%", name);
            var dt = DbHelper.GetDataTable(sql);
            dataGridView1.DataSource = dt;
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            var sql = "select * from Users";
            var sqlAil = "select * from Articles";
            var dt = DbHelper.GetDataTable(sqlAil);
            dataGridView1.DataSource = dt;
            dataGridView1.ReadOnly = true;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.AllowUserToAddRows = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form4 form4 = new Form4();
            this.Hide();
            form4.Show();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var id = dataGridView1.SelectedRows[0].Cells["Id"].Value;
                var ti = dataGridView1.SelectedRows[0].Cells["Title"].Value;
                var au = dataGridView1.SelectedRows[0].Cells["Author"].Value;
                var cr = dataGridView1.SelectedRows[0].Cells["CreatedTime"].Value;
                Form4 form4 = new Form4();
                this.Hide();
                form4.Show();
            }
            else
            {
                MessageBox.Show("无效操作");
            }
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var da = dataGridView1.SelectedRows[0];
                var id = (int)da.Cells["Id"].Value;
                var resa = MessageBox.Show("确定删除吗", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                if (resa.Equals(DialogResult.Yes))
                {
                    var sql = string.Format("delete from Articles where Id={0}", id);
                    var row = DbHelper.Operate(sql);
                    if (row > 0)
                    {
                        var sqlAli = "select * from Articles";
                        var dt = DbHelper.GetDataTable(sqlAli);
                        dataGridView1.DataSource = dt;
                        MessageBox.Show("删除成功");
                    }
                }
            }
            
        }
    }
}
